package com.qfedu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.Depart;
import com.qfedu.entity.GroupDepartData;
import com.qfedu.entity.ResponseData;
import com.qfedu.service.DepartService;
import com.qfedu.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    private DepartService departService;

    /**
     * 添加部门
     *
     * @param depart
     * @return
     */
    @RequestMapping("/addDepart.do")
    public ResponseData addDepart(Depart depart) {

        int num = departmentService.addDepartment(depart);

        ResponseData responseData = new ResponseData();

        // 判断是否添加成功
        if (num > 0) {
            responseData.setMsg("success");
            responseData.setCode(0);// 0代表成功
        } else {
            responseData.setMsg("fail");
            responseData.setCode(1);// 1 代表失败
        }
        return responseData;
    }

    /**
     * 分页查询所有部门信息
     *
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/departpage.do")
    public ResponseData findAllDepart(int page, int limit) {

        // 开启分页
        PageHelper.startPage(page, limit);

        // 按照分页查询
        List<Depart> departList = departmentService.findAllDepart();

        PageInfo departPageInfo = new PageInfo(departList);

        // 获取数据表中所有数据行数
        int count = (int) departPageInfo.getTotal();

        ResponseData responseData = new ResponseData();

        // 成功
        responseData.setCode(0);
        responseData.setMsg("success");
        responseData.setCount(count);
        responseData.setData(departList);

        return responseData;
    }

    /**
     * 根据id删除部门
     *
     * @param id
     * @return
     */
    @RequestMapping("/departdelete.do")
    public ResponseData deleteDepartById(int id) {

        boolean result = departmentService.deleteDepartById(id);

        ResponseData responseData = new ResponseData();

        if (result) {
            responseData.setMsg("success");
            responseData.setCode(0);// 0代表成功
        } else {
            responseData.setMsg("fail");
            responseData.setCode(1);// 1 代表失败
        }
        return responseData;
    }

    /**
     * 更改部门信息
     *
     * @param depart
     * @return
     */
    @RequestMapping("/departupdate.do")
    public ResponseData updateDepart(Depart depart) {

        int num = departmentService.updateDepartment(depart);

        ResponseData responseData = new ResponseData();


        if (num > 0) {
            responseData.setCode(0);// 0代表成功
            responseData.setMsg("success");
        } else {
            responseData.setCode(1);// 1 代表失败
            responseData.setMsg("fail");
        }
        return responseData;
    }


}
